home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / stl / stl_map.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-08  |  10.4 KB  |  296 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  *
  15.  * Copyright (c) 1996,1997
  16.  * Silicon Graphics Computer Systems, Inc.
  17.  *
  18.  * Permission to use, copy, modify, distribute and sell this software
  19.  * and its documentation for any purpose is hereby granted without fee,
  20.  * provided that the above copyright notice appear in all copies and
  21.  * that both that copyright notice and this permission notice appear
  22.  * in supporting documentation.  Silicon Graphics makes no
  23.  * representations about the suitability of this software for any
  24.  * purpose.  It is provided "as is" without express or implied warranty.
  25.  */
  26.  
  27. /* NOTE: This is an internal header file, included by other STL headers.
  28.  *   You should not attempt to use it directly.
  29.  */
  30.  
  31. #ifndef __SGI_STL_INTERNAL_MAP_H
  32. #define __SGI_STL_INTERNAL_MAP_H
  33.  
  34. #include <concept_checks.h>
  35.  
  36. __STL_BEGIN_NAMESPACE
  37.  
  38. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  39. #pragma set woff 1174
  40. #pragma set woff 1375
  41. #endif
  42.  
  43. // Forward declarations of operators == and <, needed for friend declarations.
  44. template <class _Key, class _Tp, 
  45.           class _Compare __STL_DEPENDENT_DEFAULT_TMPL(less<_Key>),
  46.           class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
  47. class map;
  48.  
  49. template <class _Key, class _Tp, class _Compare, class _Alloc>
  50. inline bool operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
  51.                        const map<_Key,_Tp,_Compare,_Alloc>& __y);
  52.  
  53. template <class _Key, class _Tp, class _Compare, class _Alloc>
  54. inline bool operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
  55.                       const map<_Key,_Tp,_Compare,_Alloc>& __y);
  56.  
  57. template <class _Key, class _Tp, class _Compare, class _Alloc>
  58. class map {
  59. public:
  60.  
  61. // requirements:
  62.  
  63.   __STL_CLASS_REQUIRES(_Tp, _Assignable);
  64.   __STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Key, _Key);
  65.  
  66. // typedefs:
  67.  
  68.   typedef _Key                  key_type;
  69.   typedef _Tp                   data_type;
  70.   typedef _Tp                   mapped_type;
  71.   typedef pair<const _Key, _Tp> value_type;
  72.   typedef _Compare              key_compare;
  73.     
  74.   class value_compare
  75.     : public binary_function<value_type, value_type, bool> {
  76.   friend class map<_Key,_Tp,_Compare,_Alloc>;
  77.   protected :
  78.     _Compare comp;
  79.     value_compare(_Compare __c) : comp(__c) {}
  80.   public:
  81.     bool operator()(const value_type& __x, const value_type& __y) const {
  82.       return comp(__x.first, __y.first);
  83.     }
  84.   };
  85.  
  86. private:
  87.   typedef _Rb_tree<key_type, value_type, 
  88.                    _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
  89.   _Rep_type _M_t;  // red-black tree representing map
  90. public:
  91.   typedef typename _Rep_type::pointer pointer;
  92.   typedef typename _Rep_type::const_pointer const_pointer;
  93.   typedef typename _Rep_type::reference reference;
  94.   typedef typename _Rep_type::const_reference const_reference;
  95.   typedef typename _Rep_type::iterator iterator;
  96.   typedef typename _Rep_type::const_iterator const_iterator;
  97.   typedef typename _Rep_type::reverse_iterator reverse_iterator;
  98.   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
  99.   typedef typename _Rep_type::size_type size_type;
  100.   typedef typename _Rep_type::difference_type difference_type;
  101.   typedef typename _Rep_type::allocator_type allocator_type;
  102.  
  103.   // allocation/deallocation
  104.  
  105.   map() : _M_t(_Compare(), allocator_type()) {}
  106.   explicit map(const _Compare& __comp,
  107.                const allocator_type& __a = allocator_type())
  108.     : _M_t(__comp, __a) {}
  109.  
  110. #ifdef __STL_MEMBER_TEMPLATES
  111.   template <class _InputIterator>
  112.   map(_InputIterator __first, _InputIterator __last)
  113.     : _M_t(_Compare(), allocator_type())
  114.     { _M_t.insert_unique(__first, __last); }
  115.  
  116.   template <class _InputIterator>
  117.   map(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
  118.       const allocator_type& __a = allocator_type())
  119.     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  120. #else
  121.   map(const value_type* __first, const value_type* __last)
  122.     : _M_t(_Compare(), allocator_type())
  123.     { _M_t.insert_unique(__first, __last); }
  124.  
  125.   map(const value_type* __first,
  126.       const value_type* __last, const _Compare& __comp,
  127.       const allocator_type& __a = allocator_type())
  128.     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  129.  
  130.   map(const_iterator __first, const_iterator __last)
  131.     : _M_t(_Compare(), allocator_type()) 
  132.     { _M_t.insert_unique(__first, __last); }
  133.  
  134.   map(const_iterator __first, const_iterator __last, const _Compare& __comp,
  135.       const allocator_type& __a = allocator_type())
  136.     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
  137.  
  138. #endif /* __STL_MEMBER_TEMPLATES */
  139.  
  140.   map(const map<_Key,_Tp,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
  141.   map<_Key,_Tp,_Compare,_Alloc>&
  142.   operator=(const map<_Key, _Tp, _Compare, _Alloc>& __x)
  143.   {
  144.     _M_t = __x._M_t;
  145.     return *this; 
  146.   }
  147.  
  148.   // accessors:
  149.  
  150.   key_compare key_comp() const { return _M_t.key_comp(); }
  151.   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
  152.   allocator_type get_allocator() const { return _M_t.get_allocator(); }
  153.  
  154.   iterator begin() { return _M_t.begin(); }
  155.   const_iterator begin() const { return _M_t.begin(); }
  156.   iterator end() { return _M_t.end(); }
  157.   const_iterator end() const { return _M_t.end(); }
  158.   reverse_iterator rbegin() { return _M_t.rbegin(); }
  159.   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
  160.   reverse_iterator rend() { return _M_t.rend(); }
  161.   const_reverse_iterator rend() const { return _M_t.rend(); }
  162.   bool empty() const { return _M_t.empty(); }
  163.   size_type size() const { return _M_t.size(); }
  164.   size_type max_size() const { return _M_t.max_size(); }
  165.   _Tp& operator[](const key_type& __k) {
  166.     iterator __i = lower_bound(__k);
  167.     // __i->first is greater than or equivalent to __k.
  168.     if (__i == end() || key_comp()(__k, (*__i).first))
  169.       __i = insert(__i, value_type(__k, _Tp()));
  170.     return (*__i).second;
  171.   }
  172.   void swap(map<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
  173.  
  174.   // insert/erase
  175.  
  176.   pair<iterator,bool> insert(const value_type& __x) 
  177.     { return _M_t.insert_unique(__x); }
  178.   iterator insert(iterator position, const value_type& __x)
  179.     { return _M_t.insert_unique(position, __x); }
  180. #ifdef __STL_MEMBER_TEMPLATES
  181.   template <class _InputIterator>
  182.   void insert(_InputIterator __first, _InputIterator __last) {
  183.     _M_t.insert_unique(__first, __last);
  184.   }
  185. #else
  186.   void insert(const value_type* __first, const value_type* __last) {
  187.     _M_t.insert_unique(__first, __last);
  188.   }
  189.   void insert(const_iterator __first, const_iterator __last) {
  190.     _M_t.insert_unique(__first, __last);
  191.   }
  192. #endif /* __STL_MEMBER_TEMPLATES */
  193.  
  194.   void erase(iterator __position) { _M_t.erase(__position); }
  195.   size_type erase(const key_type& __x) { return _M_t.erase(__x); }
  196.   void erase(iterator __first, iterator __last)
  197.     { _M_t.erase(__first, __last); }
  198.   void clear() { _M_t.clear(); }
  199.  
  200.   // map operations:
  201.  
  202.   iterator find(const key_type& __x) { return _M_t.find(__x); }
  203.   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
  204.   size_type count(const key_type& __x) const {
  205.     return _M_t.find(__x) == _M_t.end() ? 0 : 1; 
  206.   }
  207.   iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
  208.   const_iterator lower_bound(const key_type& __x) const {
  209.     return _M_t.lower_bound(__x); 
  210.   }
  211.   iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
  212.   const_iterator upper_bound(const key_type& __x) const {
  213.     return _M_t.upper_bound(__x); 
  214.   }
  215.   
  216.   pair<iterator,iterator> equal_range(const key_type& __x) {
  217.     return _M_t.equal_range(__x);
  218.   }
  219.   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
  220.     return _M_t.equal_range(__x);
  221.   }
  222.  
  223. #ifdef __STL_TEMPLATE_FRIENDS 
  224.   template <class _K1, class _T1, class _C1, class _A1>
  225.   friend bool operator== (const map<_K1, _T1, _C1, _A1>&,
  226.                           const map<_K1, _T1, _C1, _A1>&);
  227.   template <class _K1, class _T1, class _C1, class _A1>
  228.   friend bool operator< (const map<_K1, _T1, _C1, _A1>&,
  229.                          const map<_K1, _T1, _C1, _A1>&);
  230. #else /* __STL_TEMPLATE_FRIENDS */
  231.   friend bool __STD_QUALIFIER
  232.   operator== __STL_NULL_TMPL_ARGS (const map&, const map&);
  233.   friend bool __STD_QUALIFIER
  234.   operator< __STL_NULL_TMPL_ARGS (const map&, const map&);
  235. #endif /* __STL_TEMPLATE_FRIENDS */
  236. };
  237.  
  238. template <class _Key, class _Tp, class _Compare, class _Alloc>
  239. inline bool operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
  240.                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  241.   return __x._M_t == __y._M_t;
  242. }
  243.  
  244. template <class _Key, class _Tp, class _Compare, class _Alloc>
  245. inline bool operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
  246.                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  247.   return __x._M_t < __y._M_t;
  248. }
  249.  
  250. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  251.  
  252. template <class _Key, class _Tp, class _Compare, class _Alloc>
  253. inline bool operator!=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
  254.                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  255.   return !(__x == __y);
  256. }
  257.  
  258. template <class _Key, class _Tp, class _Compare, class _Alloc>
  259. inline bool operator>(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
  260.                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  261.   return __y < __x;
  262. }
  263.  
  264. template <class _Key, class _Tp, class _Compare, class _Alloc>
  265. inline bool operator<=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
  266.                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  267.   return !(__y < __x);
  268. }
  269.  
  270. template <class _Key, class _Tp, class _Compare, class _Alloc>
  271. inline bool operator>=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
  272.                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
  273.   return !(__x < __y);
  274. }
  275.  
  276. template <class _Key, class _Tp, class _Compare, class _Alloc>
  277. inline void swap(map<_Key,_Tp,_Compare,_Alloc>& __x, 
  278.                  map<_Key,_Tp,_Compare,_Alloc>& __y) {
  279.   __x.swap(__y);
  280. }
  281.  
  282. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  283.  
  284. #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
  285. #pragma reset woff 1174
  286. #pragma reset woff 1375
  287. #endif
  288.  
  289. __STL_END_NAMESPACE
  290.  
  291. #endif /* __SGI_STL_INTERNAL_MAP_H */
  292.  
  293. // Local Variables:
  294. // mode:C++
  295. // End:
  296.